﻿#include "ActivityEightDescEx.h"

#include <DescStore/ActivityEightActivityEightDesc.h>

#include <NFComm/NFCore/NFTimeUtility.h>
#include <NFLogicCommon/NFPublicDefine.h>

#include "proto_common_activity.nanopb.h"
#include "proto_common_activity.pb.h"

ActivityEightDescEx::ActivityEightDescEx()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

ActivityEightDescEx::~ActivityEightDescEx()
{
}

int ActivityEightDescEx::CreateInit()
{
    return 0;
}

int ActivityEightDescEx::ResumeInit()
{
    return 0;
}

int ActivityEightDescEx::Load()
{
    return 0;
}

int ActivityEightDescEx::CheckWhenAllDataLoaded()
{
    m_astGroupDesc.clear();
    int iRetCode = 0;
    auto &vecDesc = ActivityEightActivityEightDesc::Instance()->GetResDesc();
    for (int i = 0; i < (int) vecDesc.size(); i++)
    {
        auto pDesc = &vecDesc[i];
        CHECK_EXPR(proto_ff::ENActivityEightType_IsValid(pDesc->type), -1, "ActivityEightDesc type error:{}", pDesc->type);

        time_t tTime = 0;
        if (pDesc->begin_date_str.size() > 0)
        {
            iRetCode = NFTimeUtility::StrToTime(pDesc->begin_date_str.c_str(), &tTime);
            pDesc->begin_date_stamp = tTime;
        }
        else
        {
            pDesc->begin_date_stamp = 0;
        }

        if (pDesc->end_date_str.size() > 0)
        {
            iRetCode = NFTimeUtility::StrToTime(pDesc->end_date_str.c_str(), &tTime);
            pDesc->end_date_stamp = tTime;
        }
        else
        {
            pDesc->begin_date_stamp = 0;
        }

        CHECK_EXPR(pDesc->end_date_stamp >= pDesc->begin_date_stamp, -1, "groupid:{} time config error!", pDesc->group_id);

        auto pGroup = GetGroupDesc(pDesc->group_id);
        if (pGroup == NULL)
        {
            CHECK_EXPR(m_astGroupDesc.size() < m_astGroupDesc.max_size(), -1, "m_astGroupDesc full");
            m_astGroupDesc.emplace_back();
            pGroup = &m_astGroupDesc.back();
            pGroup->group_id = pDesc->group_id;
            pGroup->open_offset = pDesc->open_offset;
            pGroup->begin_date_stamp = pDesc->begin_date_stamp;
            pGroup->end_date_stamp = pDesc->end_date_stamp;
            pGroup->create_role_offset = pDesc->create_role_offset;
        }
    }

    return iRetCode;
}

ActivityEightGroupDesc *ActivityEightDescEx::GetGroupDesc(int groupId)
{
    for (int i = 0; i < (int) m_astGroupDesc.size(); i++)
    {
        if (m_astGroupDesc[i].group_id == groupId)
        {
            return &m_astGroupDesc[i];
        }
    }
    return NULL;
}

int ActivityEightDescEx::GetGroupID(int iCRTime, int iServerOpenTime, int iTimeNow, int &iActivityStartTime) const
{
    int iOpenDayStartTime = NFTimeUtility::GetTodayStartTime(iServerOpenTime);
    int iCRDayStartTime = NFTimeUtility::GetTodayStartTime(iCRTime);

    for (int i = 0; i < (int)m_astGroupDesc.size(); ++i)
    {
        if (m_astGroupDesc[i].open_offset == 0
            && m_astGroupDesc[i].create_role_offset == 0
            && m_astGroupDesc[i].begin_date_stamp == 0
            && m_astGroupDesc[i].end_date_stamp == 0)
        {
            iActivityStartTime = iCRTime;
            return m_astGroupDesc[i].group_id;
        }

        //create role limit
        if (m_astGroupDesc[i].create_role_offset > 0)
        {
            int iCRLimitTime = iCRDayStartTime + DAY(m_astGroupDesc[i].create_role_offset - 1);
            iActivityStartTime = iCRLimitTime;
        }

        //server open day limit
        if (m_astGroupDesc[i].open_offset > 0)
        {
            int iOpenLimitTime = iOpenDayStartTime + DAY(m_astGroupDesc[i].open_offset - 1);
            iActivityStartTime = std::max(iActivityStartTime, iOpenLimitTime);
        }

        if (m_astGroupDesc[i].begin_date_stamp > 0)
        {
            if (m_astGroupDesc[i].begin_date_stamp < iActivityStartTime)
            {
                return 0;
            }
            else
            {
                iActivityStartTime = m_astGroupDesc[i].begin_date_stamp;
            }

            if (iTimeNow >= iActivityStartTime && iTimeNow < iActivityStartTime + DAY(8))
            {
                return m_astGroupDesc[i].group_id;
            }
        }
    }

    return 0;
}

int ActivityEightDescEx::GetActivityGroupID(int iCRTime, int iServerOpenTime, int iTimeNow, int &iActivityStartTime) const
{
    int iOpenDayStartTime = NFTimeUtility::GetTodayStartTime(iServerOpenTime);
    int iCRDayStartTime = NFTimeUtility::GetTodayStartTime(iCRTime);

    for (int i = m_astGroupDesc.size() - 1; i >= 0; ++i)
    {
        if (m_astGroupDesc[i].open_offset == 0
            && m_astGroupDesc[i].create_role_offset == 0
            && m_astGroupDesc[i].begin_date_stamp == 0
            && m_astGroupDesc[i].end_date_stamp == 0)
        {
            iActivityStartTime = iCRTime;
            return m_astGroupDesc[i].group_id;
        }

        //create role limit
        if (m_astGroupDesc[i].create_role_offset > 0)
        {
            int iCRLimitTime = iCRDayStartTime + DAY(m_astGroupDesc[i].create_role_offset - 1);
            iActivityStartTime = iCRLimitTime;
        }

        //server open day limit
        if (m_astGroupDesc[i].open_offset > 0)
        {
            int iOpenLimitTime = iOpenDayStartTime + DAY(m_astGroupDesc[i].open_offset - 1);
            iActivityStartTime = std::max(iActivityStartTime, iOpenLimitTime);
        }

        if (m_astGroupDesc[i].begin_date_stamp > 0)
        {
            if (m_astGroupDesc[i].begin_date_stamp < iActivityStartTime)
            {
                return 0;
            }
            else
            {
                iActivityStartTime = m_astGroupDesc[i].begin_date_stamp;
            }

            if (iTimeNow >= iActivityStartTime && iTimeNow < iActivityStartTime + DAY(8))
            {
                return m_astGroupDesc[i].group_id;
            }
        }
    }

    return 0;
}
